package com.liu.netty.service;

import com.liu.netty.dao.DriverDao;
import com.liu.netty.dao.SelfCheckDao;
import com.liu.netty.entity.Driver;
import com.liu.netty.entity.SelfCheck;
import com.liu.netty.pojo.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import util.IdWorker;

import javax.transaction.Transactional;


@Slf4j
@Service
@Transactional
public class SelfCheckService {

    @Autowired
    private SelfCheckDao selfCheckDao;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private DriverDao driverDao;


    public void addSelfCheck(SelfCheck selfCheck,String objectAddress){
        Driver d =driverDao.findByObjectAddress(objectAddress);

        selfCheck.setId( idWorker.nextId()+"");
        selfCheck.setDriver(d);
        selfCheckDao.save(selfCheck);
    }

    public void addSelfCheckFromClient(Message message){
        SelfCheck selfCheck = new SelfCheck();
        BeanUtils.copyProperties(message,selfCheck);
        addSelfCheck(selfCheck,message.getObjectAddress());


    }




}
